<?php
/**
 * Created by PhpStorm.
 * User: 毛文科
 * Date: 2018\10\28 0028
 * Time: 10:03
 */
namespace app\admin\controller;
use app\admin\model\Builder;
use app\admin\model\Digui;
use think\Controller;
use think\Db;
use think\Request;
//栏目控制器
class Column extends Controller
{
    public function show_list(){
        $data = Digui::getName('column')->digui(0,0);
        $this->assign('data',count($data));
        $this->assign('cid','');
        $this->assign('lanmu',1);
        foreach ($data as $key=>$value){
            if($value['pid']==0){
                $data[$key]['pid'] = '最上级';
            }else{
                $data[$key]['pid'] = (Db::name('column')->where('id',$value['pid'])->find())['name'];
            }
            if($value['chen']>0){
                $data[$key]['name'] = str_repeat('&nbsp;&nbsp;',$value['chen']).'|'.$value['name'];
            }
        }
        $str = Builder::make('table')
            ->addHead(['button alldel','button add'],['批量删除','添加顶级栏目'],['/admin/column/del','/admin/column/show_add'],['',''],['',''])
            ->addColumn('INDEX','checkbox','<input type="checkbox"/>','','text-align: center')
            ->addColumn('id','text','ID','','text-align: center')
            ->addColumn('name','text','栏目名称')
            ->addColumn('pid','text','所属栏目','','text-align: center')
            ->addColumn('status','status','状态','','text-align: center')
            ->addRight('操作',['icon add','icon edit','icon del'],['/admin/column/show_add?pid=','/admin/column/show_edit?id=','/admin/column/del'],['添加子栏目','编辑','删除'],'','text-align: center')
            ->addData($data)
            ->fetch();
        $this->assign('str',$str);
        $this->assign('modelid','');
        $this->assign('tiaojian','');
//        echo '<pre>';print_r($str);exit;
        return $this->fetch('public/list');
    }
    public function show_add(Request $request){
        $pid = $request->param('pid',0);
        if($pid==0){
            $lanmu = '最上级';
        }else{
            $lanmu = Db::name('column')->where('id',$pid)->find();
            $lanmu = $lanmu['name'];
        }
        $str =  Builder::make('form','/admin/column/add','','')
            ->addNone('pid',$pid)
            ->addInput('','所属栏目',$lanmu,'','width:50%')
            ->addInput('name','栏目名称','','','width:50%')
            ->addInput('model','模型id','','','width:50%')
            ->addInput('english','英文名称','','','width:50%')
            ->addInput('create_url','链接地址','','','width:50%')
            ->addSelect('status','是否开启',1,[0=>'关闭',1=>'开启'])
            ->addButton(['提交','返回'],['submit','back'])
            ->fetch();
        $this->assign('str',$str);
        return $this->fetch('public/bianji');
    }
    public function show_edit(Request $request){
        $id = $request->param('id',null);
        $data = Db::name('column')->where('id',$id)->find();
        if($data['pid']==0){
            $data['pids'] = '最上级';
        }else{
            $data['pids'] = Db::name('column')->where('id',$data['pid'])->find();
            $data['pids'] = $data['pids']['name'];
        }
        $str =  Builder::make('form','/admin/column/update','','')
            ->addNone('id',$data['id'])
            ->addInput('','所属栏目',$data['pids'],'','width:50%')
            ->addInput('name','栏目名称',$data['name'],'','width:50%')
            ->addInput('model','模型id',$data['model'],'','width:50%')
            ->addInput('english','英文名称',$data['english'],'','width:50%')
            ->addInput('create_url','链接地址',$data['create_url'],'','width:50%')
            ->addSelect('status','是否开启',$data['status'],[0=>'关闭',1=>'开启'])
            ->addButton(['提交','返回'],['submit','back'])
            ->fetch();
        $this->assign('str',$str);
        return $this->fetch('public/bianji');
    }
    public function add(Request $request){
        $data = $request->post();
        if(!$data['create_url']){
            $data['create_url'] = '/admin/document/show_list';
        }
        if(Db::name('column')->insert($data)){
            return $this->fetch('public/error',['code'=>200,'action'=>'/admin/column/show_list']);
        }else{
            return $this->fetch('public/error',['code'=>203,'action'=>'/admin/column/show_list']);
        }
    }
    public function update(Request $request){
        $data = $request->post();
        if(Db::name('column')->where('id',$data['id'])->update($data)){
            return $this->fetch('public/error',['code'=>200,'action'=>'/admin/column/show_list']);
        }else{
            return $this->fetch('public/error',['code'=>203,'action'=>'/admin/column/show_list']);
        }
    }
    public function del(Request $request){
        $id = $request->post('id');
        $data = Db::name('column')->where('id','in',$id)->select();
        $arrid = [];
        foreach ($data as $value){
            $arrid[] = $value['id'];
            $arrays = Digui::getName('column')->digui($value['id']);
            foreach ($arrays as $v){
                $arrid[] = $v['id'];
            }
        }
        if(Db::name('column')->where('id','in',$arrid)->delete()){
            return json(['code'=>200,'msg'=>'删除成功']);
        }else{
            return json(['code'=>200,'msg'=>'删除失败']);
        }
    }
}